using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using DongDong.Admin;

public static class DropDownHelper
{
    //    public static void Select(this RadioButtonList theList, object theValue)
    //    {
    //        try
    //        {
    //            theList.ClearSelection();
    //            theList.Items.FindByValue(theValue.ToString()).Selected = true;
    //        }
    //        catch { }
    //    }

    //    public static string AddBRs(this string theString)
    //    {
    //        return DongDong.Common.Misc.AddBRs(theString);
    //    }

    //    public static void SelectByName(this RadioButtonList theList, object theName)
    //    {
    //        try
    //        {
    //            theList.ClearSelection();
    //            theList.Items.FindByText(theName.ToString()).Selected = true;
    //        }
    //        catch { }
    //    }

    public static void Select(this DropDownList theList, object theValue)
    {
        try
        {
            theList.ClearSelection();
            theList.Items.FindByValue(theValue.ToString()).Selected = true;
        }
        catch { }
    }

    //    public static void SelectByName(this DropDownList theList, object theName)
    //    {
    //        try
    //        {
    //            theList.ClearSelection();
    //            theList.Items.FindByText(theName.ToString()).Selected = true;
    //        }
    //        catch { }
    //    }

    //    public static int ToInt(this object theObject)
    //    {
    //        try
    //        {
    //            return Convert.ToInt32(theObject);
    //        }
    //        catch { return -1; }
    //    }

    //    public static DateTime ToDateTime(this object theObject)
    //    {
    //        return Convert.ToDateTime(theObject);
    //    }







    //    public static double ToDouble(this object theObject)
    //    {
    //        try
    //        {
    //            return Convert.ToDouble(theObject);
    //        }
    //        catch
    //        {
    //            return -1;
    //        }
    //    }

    //public static bool ToBool(this object theObject)
    //{
    //    try
    //    {
    //        return Convert.ToBoolean(theObject);
    //    }
    //    catch
    //    {
    //        return false;
    //    }
    //}

    public static void PopulateFromEnum<T>(this DropDownList drpist)
    {
        Type enumType = typeof(T);

        // Can't use type constraints on value types, so have to do check like this
        if (enumType.BaseType != typeof(Enum))
            throw new ArgumentException("T must be of type System.Enum");

        Array enumValArray = Enum.GetValues(enumType);

        foreach (string s in Enum.GetNames(enumType))
            drpist.Items.Add(new ListItem(s.Replace("_", " ").SplitUpperCaseToString(), Convert.ToInt16(Enum.Parse(enumType, s)).ToString()));
    }
    public static void PopulateFromEnum<T>(this ListView listview)
    {
        Type enumType = typeof(T);

        // Can't use type constraints on value types, so have to do check like this
        if (enumType.BaseType != typeof(Enum))
            throw new ArgumentException("T must be of type System.Enum");

        Array enumValArray = Enum.GetValues(enumType);
        List<DateSourceHelper> DateSourceHelpers = new List<DateSourceHelper>();
        foreach (string s in Enum.GetNames(enumType))
        {
            DateSourceHelper objDateSourceHelper = new DateSourceHelper();
            objDateSourceHelper.Value = Convert.ToInt16(Enum.Parse(enumType, s)).ToString();
            objDateSourceHelper.Text = s.Replace("_", " ").SplitUpperCaseToString();
            DateSourceHelpers.Add(objDateSourceHelper);
        }
        listview.DataSource = DateSourceHelpers;
        listview.DataBind();

    }

    public static void PopulateFromEnum<T>(this Repeater rpt)
    {
        Type enumType = typeof(T);

        // Can't use type constraints on value types, so have to do check like this
        if (enumType.BaseType != typeof(Enum))
            throw new ArgumentException("T must be of type System.Enum");

        Array enumValArray = Enum.GetValues(enumType);
        List<DateSourceHelper> DateSourceHelpers = new List<DateSourceHelper>();
        foreach (string s in Enum.GetNames(enumType))
        {
            DateSourceHelper objDateSourceHelper = new DateSourceHelper();
            objDateSourceHelper.Value = Convert.ToInt16(Enum.Parse(enumType, s)).ToString();
            objDateSourceHelper.Text = s.Replace("_", " ").SplitUpperCaseToString();
            DateSourceHelpers.Add(objDateSourceHelper);
        }
        rpt.DataSource = DateSourceHelpers;
        rpt.DataBind();
    }



    public static void PopulateFromEnum<T>(this CheckBoxList drpist)
    {
        Type enumType = typeof(T);

        // Can't use type constraints on value types, so have to do check like this
        if (enumType.BaseType != typeof(Enum))
            throw new ArgumentException("T must be of type System.Enum");

        Array enumValArray = Enum.GetValues(enumType);

        foreach (string s in Enum.GetNames(enumType))
            drpist.Items.Add(new ListItem(s.Replace("_", " ").SplitUpperCaseToString(), Convert.ToInt16(Enum.Parse(enumType, s)).ToString()));
    }

    public static void PopulateFromEnum<T>(this RadioButtonList drpist)
    {
        Type enumType = typeof(T);

        // Can't use type constraints on value types, so have to do check like this
        if (enumType.BaseType != typeof(Enum))
            throw new ArgumentException("T must be of type System.Enum");

        Array enumValArray = Enum.GetValues(enumType);

        foreach (string s in Enum.GetNames(enumType))
            drpist.Items.Add(new ListItem(s.Replace("_", " ").SplitUpperCaseToString(), Convert.ToInt16(Enum.Parse(enumType, s)).ToString()));
    }

    public static string ToW3CValid(this object url)
    {
        if (url != null)
        {
            string stripUrl = url.ToString();
            try
            {
                stripUrl = stripUrl.Replace("'", "");
                stripUrl = stripUrl.Replace("&rsquos", "&rsquos;");
                stripUrl = stripUrl.Replace("&", "&amp;");
            }
            catch { }

            return stripUrl;
        }
        else
        {
            return "";
        }
    }

    public static string ToValidUrl(this object url)
    {
        string stripUrl = "";

        try
        {
            stripUrl = url.ToString();
            stripUrl = stripUrl.Replace(" ", "_");
            stripUrl = stripUrl.Replace("&", "");
            stripUrl = stripUrl.Replace("'", "");
            stripUrl = stripUrl.Replace("\"", "");
            stripUrl = stripUrl.Replace("/", "");
            stripUrl = stripUrl.Replace(":", "");
            stripUrl = stripUrl.Replace("#", "");
            stripUrl = stripUrl.Replace("?", "");
            stripUrl = stripUrl.Replace("<", "");
            stripUrl = stripUrl.Replace(">", "");
            stripUrl = stripUrl.Replace("-", "_");
            stripUrl = stripUrl.Replace(",", "_");
            stripUrl = stripUrl.ToLower();
            // stripUrl = stripUrl.Replace(".", "");

            //   if (stripUrl.Length > 30)
            //      stripUrl = stripUrl.Substring(0, 30);
        }
        catch { }

        return stripUrl;
    }

    /// <summary>
    /// Parse a string into an enumeration
    /// </summary>
    /// <typeparam name="TEnum">The Enumeration type to cast to</typeparam>
    /// <param name="source"></param>
    /// <returns></returns>
    public static TEnum ParseEnum<TEnum>(this string source)
    {
        Type t = typeof(TEnum);

        if (!t.IsEnum)
            throw new ArgumentException("TEnum must be a valid Enumeration", "TEnum");

        return (TEnum)Enum.Parse(t, source);
    }

    public static string TrimBefore(this string source, string match)
    {
        return source.Substring(source.IndexOf(match) + match.Length);
    }

    public static string TrimAfter(this string source, string match)
    {
        return source.Substring(source.IndexOf(match) + match.Length);
    }

    /// <summary>
    /// Parses a camel cased or pascal cased string and returns a new 
    /// string with spaces between the words in the string.
    /// </summary>
    /// <example>
    /// The string "PascalCasing" will return an array with two 
    /// elements, "Pascal" and "Casing".
    /// </example>
    /// <param name="source"></param>
    /// <returns></returns>
    public static string SplitUpperCaseToString(this string source)
    {
        return string.Join(" ", SplitUpperCase(source));
    }

    /// <summary>
    /// Parses a camel cased or pascal cased string and returns an array 
    /// of the words within the string.
    /// </summary>
    /// <example>
    /// The string "PascalCasing" will return an array with two 
    /// elements, "Pascal" and "Casing".
    /// </example>
    /// <param name="source"></param>
    /// <returns></returns>
    public static string[] SplitUpperCase(this string source)
    {
        if (source == null)
            return new string[] { }; //Return empty array.

        if (source.Length == 0)
            return new string[] { "" };

        System.Collections.Specialized.StringCollection words = new System.Collections.Specialized.StringCollection();
        int wordStartIndex = 0;

        char[] letters = source.ToCharArray();

        // Skip the first letter. we don't care what case it is.
        for (int i = 1; i < letters.Length; i++)
        {
            if (char.IsUpper(letters[i]))
            {
                if (i + 1 < letters.Length && !char.IsUpper(letters[i + 1]))
                {
                    //Grab everything before the current index.
                    words.Add(new String(letters, wordStartIndex, i - wordStartIndex));
                    wordStartIndex = i;
                }
            }
        }

        //We need to have the last word.
        words.Add(new String(letters, wordStartIndex, letters.Length - wordStartIndex));

        //Copy to a string array.
        string[] wordArray = new string[words.Count];
        words.CopyTo(wordArray, 0);
        return wordArray;
    }
}

namespace DongDong.Admin
{
    //public static class DateTimeHelper
    //{
    //    /// <summary>
    //    /// Given a datetime object will return a datetime object for the monday of that week.
    //    /// If argument datetime is a Monday then the return object will be a copy of the input argument.
    //    /// </summary>
    //    /// <param name="dateTime"></param>
    //    /// <returns></returns>
    //    public static DateTime FindMonday(this DateTime dateTime)
    //    {
    //        DateTime rtnDate = dateTime;

    //        while (rtnDate.DayOfWeek != DayOfWeek.Monday)
    //            rtnDate = rtnDate.AddDays(-1);

    //        return rtnDate;
    //    }

    //    /// <summary>
    //    /// Given datetime object returns a datetime object for the first day for the month of the argument datetime
    //    /// </summary>
    //    /// <param name="dateTime"></param>
    //    /// <returns></returns>
    //    public static DateTime FindFirstOfMonth(this DateTime dateTime)
    //    {
    //        DateTime rtnVal = dateTime;

    //        while (rtnVal.Day != 1)
    //            rtnVal = rtnVal.AddDays(-1);

    //        return rtnVal;
    //    }

    //    /// <summary>
    //    /// Return datetime object for the first day of the year for the datetime argument
    //    /// </summary>
    //    /// <param name="dateTime"></param>
    //    /// <returns></returns>
    //    public static DateTime FindFirstOfYear(this DateTime dateTime)
    //    {
    //        DateTime rtnVal = dateTime;

    //        while (rtnVal.DayOfYear != 1)
    //            rtnVal = rtnVal.AddDays(-1);

    //        return rtnVal;
    //    }
    //}

    //public static string ToGBP(this object theObject)
    //{
    //    return Strings.GBPFormat(theObject);
    //}

    public class DateSourceHelper
    {
        public string Value { get; set; }
        public string Text { get; set; }


    }
}

